<?php

namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Storage;
use FFMpeg;
use Illuminate\Support\Facades\DB;
use Cache;
use Validator;
use Carbon\Carbon;
use  App\Models\Phone;
class UplaodController extends Controller
{
    public function upload(Request $request)
    {
        $type = $request->input('type');
        $file = $request->file('file');
        // if($type=='image'){
        //     $file = $request->image('file');
        // }
        if ($file && $type){
            //原文件名
            $originalName = $file->getClientOriginalName();
            // $data['imei'] = '11'.$originalName;
            // $data['version'] = 11;
            //扩展名
            $ext = $file->getClientOriginalExtension();
            //MimeType
            // $type = $file->getClientMimeType();
            //临时绝对路径
            $realPath = $file->getRealPath();
            if($type=='image'&&empty($ext)){
                $ext = 'jpg';
            }
            $filename = uniqid().'.'.$ext;
            if($type == 'audio_tran'){
                $type = 'audio';
                $bool = Storage::disk($type)->put($filename,file_get_contents($realPath));
                $data = $this->audio_tran($ext,'/var/www/html/PhoneHook/public/uploads/audio/'.date('Ymd').'/',$filename);
                return $this->returnOk($data);
            }
            $bool = Storage::disk($type)->put($filename,file_get_contents($realPath));
            if($bool){
                $data['file_url'] = 'http://47.105.196.123:8088/uploads/'.$type.'/'.date('Ymd').'/'.$filename;
                return $this->returnOk($data);
            }else{
                return $this->returnError([],409,'上传失败');
            }
        }
        return $this->returnError([],403,'参数错误');
    }

    public function audio_tran($ext,$audio_path,$file_name)
    {
        $ffmpeg = \FFMpeg\FFMpeg::create(array(
            'ffmpeg.binaries'  => '/usr/bin/ffmpeg',
            'ffprobe.binaries' => '/usr/bin/ffprobe',
            'timeout'          => 3600, // The timeout for the underlying process
            'ffmpeg.threads'   => 12,   // The number of threads that FFMpeg should use
        ));
        $time = time();
        if($ext =='amr' || $ext=='3gp'){
            $audio = $ffmpeg->open($audio_path.$file_name);
            $format = new FFMpeg\Format\Audio\Wav();
            $format->setAudioChannels(1);
            $audio->filters()->resample('16000');
            $wav_filename = $time.'.wav';
            $audio->save($format, $audio_path.$wav_filename);
            $data['file_url'] = 'http://47.105.196.123:8088/uploads/audio/'.date('Ymd').'/'.$wav_filename;
            return $data;
        }else{
            $amr_filename = $time.'.amr';
            exec('ffmpeg -i '.$audio_path.$file_name.'  -ab 12.2k -ar 8000 -ac 1 '.$audio_path.$amr_filename);
            $data['file_url'] = 'http://47.105.196.123:8088/uploads/audio/'.date('Ymd').'/'.$amr_filename;
            return $data;
        }
    }
}